package com.itheima.reggie.web.exception;

import com.itheima.reggie.common.R;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
public class GlobalExceptionHandle  {
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R doSQLIntegrityConstraintViolationException(SQLIntegrityConstraintViolationException exception){

        if ("Duplicate entry".contains("Duplicate entry")){
            String[] split = exception.getMessage().split(" ");
            String msg=split[2]+"已存在";
           return R.error(msg);
        }
        return R.error("未知错误");
    }

    @ExceptionHandler(CustomException.class)
    public R doSQLIntegrityConstraintViolationException(CustomException exception){

        return R.error(exception.getMessage());
    }
}
